package com.efast.dmom.equipment.service.masterdata.impl;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efast.dmom.equipment.bean.EquipStatusSumBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.equipment.bean.EquipMonthlyPlanBean;
import com.efast.dmom.equipment.dao.masterdata.EquipReportDao;
import com.efast.dmom.equipment.service.masterdata.EquipReportService;

@Service
public class EquipReportServiceImpl implements EquipReportService{

	@Autowired
	private EquipReportDao equipReportDao;

	@Override
	public Map<String,Integer> EquipUseView() {
		List<EquipStatusSumBean>  list=equipReportDao.selectDebugCount();
		Integer debug=0;
		Integer running=0;
		Integer fault=0;
		Integer idle=0;
		Integer discard=0;
		if(list!=null&&list.size()>0){
			for(EquipStatusSumBean e:list){
				if("debug".equals(e.getStatus())){
					debug+=e.getSum();
				}else if("running".equals(e.getStatus())){
					running+=e.getSum();
				}else if("fault".equals(e.getStatus())){
					fault+=e.getSum();
				}else if("idle".equals(e.getStatus())){
					idle+=e.getSum();
				}else if("discard".equals(e.getStatus())){
					discard+=e.getSum();
				}
			}
		}
		Map<String,Integer> map=new HashMap<>();
		//调试状态数量
        map.put("debug", debug);
       	//运行状态数量
        map.put("running", running);
       	//故障状态数量
        map.put("fault", fault);
       	//闲置状态数量
        map.put("idle", idle);
       	//报废状态数量
        map.put("discard", discard);
       	return map;
	}

	@Override
	public EquipMonthlyPlanBean EquipRepairMonthlyPlan() {
		EquipMonthlyPlanBean repairPlan=new EquipMonthlyPlanBean();
		List<EquipStatusSumBean>  list=equipReportDao.selectRepairTotalPlans();
		Integer totalPlans=0;
		Integer completed=0;
		Integer processing=0;
		Integer uncompleted=0;
		if(list!=null&&list.size()>0){
			for(EquipStatusSumBean e:list){
				totalPlans+=e.getSum();
				if("todoing".equals(e.getStatus())){
					processing+=e.getSum();
				}else if("audited".equals(e.getStatus())){
					completed+=e.getSum();
				}else if("audit".equals(e.getStatus()) || "todo".equals(e.getStatus()) || "completed".equals(e.getStatus())){
					uncompleted+=e.getSum();
				}
			}
		}
		repairPlan.setTotalPlans(totalPlans);
		repairPlan.setCompleted(completed);
		repairPlan.setProcessing(processing);
		repairPlan.setUncompleted(uncompleted);
		String completePercent="";
		//计算完成率
		  if(totalPlans != 0) {
			 NumberFormat numberFormat = NumberFormat.getInstance();
			 numberFormat.setMaximumFractionDigits(2);
			  completePercent=numberFormat.format((completed/(totalPlans*1.0))*100);
		 }else {
			 completePercent="0";
		 }
		repairPlan.setCompletePercent(completePercent);
		return repairPlan;
	}

	@Override
	public EquipMonthlyPlanBean EquipMaintainMonthlyPlan() {
		EquipMonthlyPlanBean repairPlan=new EquipMonthlyPlanBean();
		List<EquipStatusSumBean>  list=equipReportDao.selectMaintainTotalPlans();
		Integer totalPlans=0;
		Integer completed=0;
		Integer processing=0;
		Integer uncompleted=0;
		if(list!=null&&list.size()>0){
			for(EquipStatusSumBean e:list){
				totalPlans+=e.getSum();
				if("todoing".equals(e.getStatus())){
					processing+=e.getSum();
				}else if("completed".equals(e.getStatus())){
					completed+=e.getSum();
				}else if("audit".equals(e.getStatus()) || "todo".equals(e.getStatus())){
					uncompleted+=e.getSum();
				}
			}
		}
		repairPlan.setTotalPlans(totalPlans);
		repairPlan.setCompleted(completed);
		repairPlan.setProcessing(processing);
		repairPlan.setUncompleted(uncompleted);
		String completePercent="";
		//计算完成率
		if(totalPlans != 0) {
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			completePercent=numberFormat.format((completed/(totalPlans*1.0))*100);
		}else {
			completePercent="0";
		}
		repairPlan.setCompletePercent(completePercent);
		return repairPlan;
	}
}
	


	


